package com.ruoyi.system.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PwsStatus;
import com.ruoyi.system.domain.PwsTable;
import com.ruoyi.system.domain.vo.PwsQueueAndPwsTableVo;
import com.ruoyi.system.service.PwsStatusService;
import com.ruoyi.system.service.PwsTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PwsQueueMapper;
import com.ruoyi.system.domain.PwsQueue;
import com.ruoyi.system.service.IPwsQueueService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-20
 */
@Service
public class PwsQueueServiceImpl extends ServiceImpl<PwsQueueMapper,PwsQueue> implements IPwsQueueService
{
    @Autowired
    private PwsQueueMapper pwsQueueMapper;
    @Autowired
    PwsTableService pwsTableService;
    @Autowired
    PwsStatusService pwsStatusService;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public PwsQueue selectPwsQueueById(Long id)
    {
        return pwsQueueMapper.selectPwsQueueById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param pwsQueue 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<PwsQueue> selectPwsQueueList(PwsQueue pwsQueue,Integer pageNum,Integer pageSize)
    {
        LambdaQueryWrapper<PwsQueue> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(pwsQueue.getOrderNo())){
            queryWrapper.eq(PwsQueue::getOrderNo,pwsQueue.getOrderNo());
        }
        if(pwsQueue.getLineNo()!=null){
            queryWrapper.eq(PwsQueue::getLineNo,pwsQueue.getLineNo());
        }
        if(pwsQueue.getPwsStatus()!=null){
            queryWrapper.eq(PwsQueue::getPwsStatus,pwsQueue.getPwsStatus());
        }
        if(pwsQueue.getSequence()!=null){
            queryWrapper.eq(PwsQueue::getSequence,pwsQueue.getSequence());
        }
        if(pwsQueue.getCreateTime()!=null){
            queryWrapper.eq(PwsQueue::getCreateTime,pwsQueue.getCreateTime());
        }
        List<PwsQueue> pwsQueueList = baseMapper.selectPage(new Page(pageNum,pageSize),queryWrapper).getRecords();
        return pwsQueueList;
    }

    @Override
    public Long selectPwsQueueCount(PwsQueue pwsQueue) {
        LambdaQueryWrapper<PwsQueue> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(pwsQueue.getOrderNo())){
            queryWrapper.eq(PwsQueue::getOrderNo,pwsQueue.getOrderNo());
        }
        if(pwsQueue.getLineNo()!=null){
            queryWrapper.eq(PwsQueue::getLineNo,pwsQueue.getLineNo());
        }
        if(pwsQueue.getPwsStatus()!=null){
            queryWrapper.eq(PwsQueue::getPwsStatus,pwsQueue.getPwsStatus());
        }
        if(pwsQueue.getSequence()!=null){
            queryWrapper.eq(PwsQueue::getSequence,pwsQueue.getSequence());
        }
        if(pwsQueue.getCreateTime()!=null){
            queryWrapper.eq(PwsQueue::getCreateTime,pwsQueue.getCreateTime());
        }
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public List<PwsQueue> selectPwsQueueAll(PwsQueue pwsQueue)
    {
        return pwsQueueMapper.selectPwsQueueList(pwsQueue);
    }


    /**
     * 新增【请填写功能名称】
     * 
     * @param pwsQueue 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertPwsQueue(PwsQueue pwsQueue)
    {
        pwsQueue.setCreateTime(LocalDateTime.now());
        return pwsQueueMapper.insertPwsQueue(pwsQueue);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param pwsQueue 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updatePwsQueue(PwsQueue pwsQueue)
    {
        return pwsQueueMapper.updatePwsQueue(pwsQueue);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deletePwsQueueByIds(Long[] ids)
    {
        return pwsQueueMapper.deletePwsQueueByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deletePwsQueueById(Long id)
    {
        return pwsQueueMapper.deletePwsQueueById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addPwsQueue(String orderNo, Integer lineNo, Long sequence) {
        PwsQueue pwsQueue = new PwsQueue();
        pwsQueue.setOrderNo(orderNo);
        pwsQueue.setLineNo(lineNo);
        //获取该线号最大的sequence
        Long maxSequence = this.selectMaxSequence(lineNo);
        pwsQueue.setSequence(sequence+maxSequence);
        pwsQueue.setPwsStatus(1);
        pwsQueue.setCreateTime(LocalDateTime.now());
        this.baseMapper.insert(pwsQueue);

        //修改pws_status
        PwsStatus pwsStatus= pwsStatusService.selectByOrderNo(orderNo);
        pwsStatus.setIsReady(2);
        pwsStatusService.updatePwsStatus(pwsStatus);

        return true;
    }
    public  Long selectMaxSequence(Integer lineNo) {
        LambdaQueryWrapper<PwsQueue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PwsQueue::getLineNo,lineNo);
        queryWrapper.orderByDesc(PwsQueue::getSequence);
        PwsQueue pwsQueue = this.baseMapper.selectOne(queryWrapper);
        if(pwsQueue==null){
            return 0l;
        }
        return pwsQueue.getSequence();
    }

    @Override
    public PwsQueue selectPwsQueueStatusByOrderNo(String orderNo, Integer status) {
     LambdaQueryWrapper<PwsQueue> queryWrapper = new LambdaQueryWrapper<>();
     queryWrapper.eq(PwsQueue::getOrderNo,orderNo);
     queryWrapper.eq(PwsQueue::getPwsStatus,status);
     PwsQueue pwsQueue = this.baseMapper.selectOne(queryWrapper);
        return pwsQueue;
    }

    @Override
    public PwsQueueAndPwsTableVo selectpwsQueueAndPwsTableByOrderNo(String orderNo) {
        PwsQueueAndPwsTableVo pwsQueueAndPwsTableVo=new PwsQueueAndPwsTableVo();
        //获得pwsQueue
        PwsQueue pwsQueue = this.selectPwsQueueByOrderNo(orderNo);
        pwsQueueAndPwsTableVo.setPwsQueueVo(pwsQueue);
        //获得pwsTable
        PwsTable pwsTable = pwsTableService.getPwsTableByOrderNo(orderNo);
        pwsQueueAndPwsTableVo.setPwsTableVo(pwsTable);

        return pwsQueueAndPwsTableVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delPwsQueueByOrderNo(String orderNo) {
        //相同line_no后面的单子sequence需要递减
        pwsQueueMapper.updatePwsQueueSequence(orderNo,-1);
        //pws_status中的is_ready和pws_queue中的pws_status同步更新为0
         PwsQueue pwsQueue = this.selectPwsQueueByOrderNo(orderNo);
         pwsQueue.setPwsStatus(0);
         this.baseMapper.updateById(pwsQueue);
        //修改pws_status
        PwsStatus pwsStatus= pwsStatusService.selectByOrderNo(orderNo);
        pwsStatus.setIsReady(0);
        pwsStatusService.updatePwsStatus(pwsStatus);
        //删除对应单号数据
        LambdaQueryWrapper<PwsQueue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PwsQueue::getOrderNo,orderNo);
        this.baseMapper.delete(queryWrapper);

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePwsQueueByOrderNo(String orderNo ,long sequence) {
        Long oldSequence = 0l;
        PwsQueue pwsQueue = this.selectPwsQueueByOrderNo(orderNo);
        oldSequence = pwsQueue.getSequence();
        pwsQueue.setSequence(sequence);
        this.baseMapper.updateById(pwsQueue);

        pwsQueueMapper.updatePwsQueueSequence(orderNo,(int)sequence);
        return true;
    }

    @Override
    public int updateStatus(Long id) {
        LambdaUpdateWrapper<PwsQueue> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(PwsQueue::getPwsQueueId,id);
        queryWrapper.set(PwsQueue::getPwsStatus,1);
        return this.baseMapper.update(new PwsQueue(),queryWrapper);
    }

    public PwsQueue selectPwsQueueByOrderNo(String orderNo){
        LambdaQueryWrapper<PwsQueue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PwsQueue::getOrderNo,orderNo);
        PwsQueue pwsQueue = this.baseMapper.selectOne(queryWrapper);
        return pwsQueue;
    }
}
